package v3.object.ship;

import galaxy.obj.Point;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import v3.object.STcfg;
import v3.object.command.list.Analysis;
import v3.object.command.list.Impulse;
import v3.object.command.list.LongSensor;
import v3.object.command.list.Manuver;
import v3.object.command.list.Shield;
import v3.object.command.list.ShortSensor;
import v3.object.command.list.ShowSector;
import v3.object.command.list.Warp;
import v3.object.ship.weapon.WPhaser;
import v3.object.ship.weapon.WThorpedo;
import v3.object.ship.weapon.iWeapon;
import v3.object.space.Enemy;
import v3.object.space.SpaceObjType;
import v3.object.space.Universe;
import v3.object.space.aSpaceObj;
import v3.object.space.iSpaceObj;

public class Ship extends aSpaceObj
{

	Universe universe;
	int universeSize;
	public double energy;
	public int thorpedo;
	public int score;
	public double shield;
	public double hull;
	

	public Allert allert = Allert.GREEN;

	double maxShield = 100;
	double maxHull = 100;
	double maxEnergy = 10000;
	int maxTorpedos = 100000;
	double divergencePhaser=6;
	private STcfg cfg;

	private Map<Class, Double> energyConsume;
	private Map<Class, Double> timeConsume;
	private Map<Double, String> cptLong;
	private List<String> cptLongArray;
	private Map<Long,Enemy> enemyList = new HashMap<Long, Enemy>();
	
	private Map<String, iWeapon> availableWeapon;
	private int shortSensorRangeSize = 4; //Short sensor viewField 11x11
	private double efficenceAnalysys=.92; //cumulative probability of a successful analysis 
	boolean shieldON=false;
	int longSensorSensibility = 4 ;//sectors grouped to calculate the value it is expressed in half 4 means 4+1+4 sectors
	

	public Ship(Universe universe, STcfg cfg) throws Exception
	{
		setUpShip();
		this.cfg = cfg;
		this.universe = universe;
		this.universeSize = universe.getUniverseSize();
		this.spaceObjType = SpaceObjType.Enterprise;
		setRNDPosition();
		refillShip();
	}

	private void setUpShip()
	{
		cptLong = new TreeMap<Double, String>();
		cptLongArray= new ArrayList<String>();

		//ENERGY
		energyConsume = new HashMap<Class, Double>();
		energyConsume.put(new ShortSensor().getClass(), 1.);
		energyConsume.put(new Analysis().getClass(), 2.);
		energyConsume.put(new Impulse().getClass(), 3.);
		energyConsume.put(new Shield().getClass(), 5.);
		energyConsume.put(new LongSensor().getClass(), 2.);
		energyConsume.put(new Warp().getClass(), 4.);
		energyConsume.put(new ShowSector().getClass(), 2.);
		energyConsume.put(new Manuver().getClass(), 1.);
		
		
		/*
		energyConsume.put(new Attack().getClass(), 5.);
		energyConsume.put(new Thorpedo().getClass(), 5.);
		energyConsume.put(new Phaser().getClass(), 50.);/**/

		
		
		//TIME
		timeConsume = new HashMap<Class, Double>();
		timeConsume.put(new ShortSensor().getClass(), .01);
		timeConsume.put(new Analysis().getClass(), .05);
		timeConsume.put(new Impulse().getClass(), 8.);
		timeConsume.put(new Shield().getClass(), .01);
		timeConsume.put(new LongSensor().getClass(), .01);
		timeConsume.put(new Warp().getClass(), 20.);
		timeConsume.put(new ShowSector().getClass(), .01);
		timeConsume.put(new Manuver().getClass(), .11);
		/*
		timeConsume.put(new Attack().getClass(), .11);
		timeConsume.put(new Thorpedo().getClass(), .11);
		timeConsume.put(new Phaser().getClass(), .11);/**/
		
		loadAwailableWeapon();
		
		

	}

	private void loadAwailableWeapon()
	{
		availableWeapon = new HashMap<String, iWeapon>();
		availableWeapon.put("Thorpedo",new WThorpedo(30, 20, 20, 35));
		availableWeapon.put("Phaser",new WPhaser(100, 120, 20, 40));
	}

	private void refillShip()
	{
		shieldON=false;
		thorpedo = maxTorpedos;
		energy = maxEnergy;
		shield = maxShield;
		hull = maxHull;

	}

	private void setRNDPosition() throws Exception
	{
		if(cfg.isFixPosition())
		{	
			position=new Point(universeSize/2,universeSize/2);
			sectorPosition = universe.getSector(position).getFreeSectorPoint();
			universe.getSector(position).setISpaceObj(sectorPosition, this);
			return;
		}
		else
		{
			try
			{
				position = universe.getEmptyPoint();
				sectorPosition = universe.getSector(position).getFreeSectorPoint();
				universe.getSector(position).setISpaceObj(sectorPosition, this);
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}
		universe.setObject(this);
	}

	@Override
	public String getInfo()
	{
		return null;
	}

	public int getUniverseSize()
	{
		return universeSize;
	}

	public void setUniverseSize(int universeSize)
	{
		this.universeSize = universeSize;
	}

	public double getEnergyConsume(Class c)
	{
		Double res = energyConsume.get(c);
		if (res==null)
		{
			System.out.println("### ERROR - no Energy consume available for the command " + c.getName());
			return -9999;
		}
			
		return res;
	}

	public double getTimeConsume(Class c)
	{
		
		Double res = timeConsume.get(c);;
		if (res==null)
		{
			System.out.println("### ERROR - no Time consume available for the command " + c.getName());
			return -9999;
		}
			
		return res;
	}

	public Map<Double, String> getCptLong()
	{
		return cptLong;
	}

	public void setCptLong(Map<Double, String> cptLong)
	{
		this.cptLong = cptLong;
	}

	public List<String> getCptLongArray()
	{
		return cptLongArray;
	}

	public void setCptLongArray(List<String> cptLongArray)
	{
		this.cptLongArray = cptLongArray;
	}

	public void energyUpdate(double e)
	{
		this.energy=this.energy-e;		
	}

	public Map<Long,Enemy> getEnemyList()
	{
		return enemyList;
	}

	public void setEnemyList(Map<Long,Enemy> enemyList)
	{
		this.enemyList = enemyList;
	}


	public void setGREENallert()
	{
		allert=Allert.GREEN;
		enemyList = new HashMap<Long, Enemy>();
	}

	public Map<Class, Double> getTimeConsume()
	{
		return timeConsume;
	}

	public void setTimeConsume(Map<Class, Double> timeConsume)
	{
		this.timeConsume = timeConsume;
	}

	public Map<String, iWeapon> getAvailableWeapon()
	{
		return availableWeapon;
	}

	public void setAvailableWeapon(Map<String, iWeapon> availableWeapon)
	{
		this.availableWeapon = availableWeapon;
	}

	public double getDivergencePhaser()
	{
		return divergencePhaser;
	}

	public void setDivergencePhaser(double divergencePhaser)
	{
		this.divergencePhaser = divergencePhaser;
	}

	public void setREDallert()
	{
		allert=Allert.RED;
		
	}
	
	public boolean isREDallert()
	{
		return allert.equals(Allert.RED);
	}

	public int getSSsize()
	{
		return shortSensorRangeSize;
	}

	public double getEfficenceAnalysys()
	{
		return efficenceAnalysys;
	}
	
	@Override
	public String getDescription()
	{
		return spaceObjType.toString();
	}

	public boolean isShieldON()
	{
		return shieldON;
	}

	public void setShieldON(boolean shieldON)
	{
		this.shieldON = shieldON;
	}

	public double getShield()
	{
		return shield;
	}

	public void setShield(double shield)
	{
		this.shield = shield;
	}

	public int getShortSensorRangeSize()
	{
		return shortSensorRangeSize;
	}

	public void setShortSensorRangeSize(int shortSensorRangeSize)
	{
		this.shortSensorRangeSize = shortSensorRangeSize;
	}

	public int getLongSensorSensibility()
	{
		return longSensorSensibility;
	}

	public void setLongSensorSensibility(int longSensorSensibility)
	{
		this.longSensorSensibility = longSensorSensibility;
	}

	public double getHull()
	{
		return hull;
	}

	public void setHull(double hull)
	{
		this.hull = hull;
	}

	@Override
	public void collision(iSpaceObj o)
	{
		{
			if(o.getType()==SpaceObjType.Star)
			{
				hull=hull-maxHull*.8;
				shield=shield-maxShield*.8;
			}
			if(o.getType()==SpaceObjType.Enemy)
			{

				hull=hull-maxHull*.4;
				shield=shield-maxShield*.4;
			}
			if(o.getType()==SpaceObjType.StarBase)
			{

				hull=hull-maxHull*.2;
				shield=shield-maxShield*.2;
			}
			isAlive();
		}			
	}

}
